package cn.keniykey.mc.landterritory.land;

import java.util.HashMap;
import java.util.UUID;

import javax.annotation.Nullable;

import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import cn.keniykey.mc.generallibrary.input.api.EInputResult;
import cn.keniykey.mc.generallibrary.input.api.IInputRequestManager;
import cn.keniykey.mc.generallibrary.input.api.IInputResultHandler;
import cn.keniykey.mc.generallibrary.transfer.api.ETransferResult;
import cn.keniykey.mc.generallibrary.transfer.api.ITransferRequestManager;
import cn.keniykey.mc.generallibrary.transfer.api.ITransferResultHandler;
import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.landview.LandViewUtil;
import cn.keniykey.mc.landterritory.vault.Perm;

public final class LandTransferUtil {
	static HashMap<UUID,Long> hashmap_transfer_countdown = new HashMap<>();
	
	public static void transferLand(final Player player,final Land land,final boolean use_countdown,
			final boolean check_safe) {
		if(land.isDeleted()) {
			player.sendMessage(LanguageManager.getChatLang(1201010));
			return;
		}
		if(ConfigurationManager.View_LandInfo_Transfer_Per != null && 
				!Perm.getPerm().hasPermission(land.getWorldName(), player, ConfigurationManager.View_LandInfo_Transfer_Per)) {
			player.sendMessage(LanguageManager.getChatLang(1201000));
			return;
		}
		final UUID player_uuid = player.getUniqueId();
		if(use_countdown && ConfigurationManager.View_LandInfo_Transfer_Countdown > 0 && !player.isOp()) {
			final long nowtime = System.currentTimeMillis();
			if(hashmap_transfer_countdown.containsKey(player_uuid)) {
				final long millis = (nowtime - hashmap_transfer_countdown.get(player_uuid));
				if(millis < (ConfigurationManager.View_LandInfo_Transfer_Countdown * 1000)) {
					player.sendMessage(LanguageManager.getChatLang(1201001, (millis / 1000), (millis % 1000)));
					return;
				}
			}else {
				hashmap_transfer_countdown.put(player_uuid, nowtime);
			}
		}
		final int transfer_wait;
		if(ConfigurationManager.View_LandInfo_Transfer_Wait > 0 && ConfigurationManager.View_LandInfo_Transfer_Per != null &&
				!Perm.getPerm().hasPermission(land.getWorldName(), player, ConfigurationManager.View_LandInfo_Transfer_VipPer)) {
			transfer_wait = ConfigurationManager.View_LandInfo_Transfer_Wait;
		}else {
			transfer_wait = 0;
		}
		final Location landpos = land.getPos().toLocation();
		if(landpos == null) {
			player.sendMessage(LanguageManager.getChatLang(1201011));
			return;
		}
		ITransferResultHandler handler = new ITransferResultHandler() {
			private final Land land1 = land;
			@Override
			public void onTransferResult(OfflinePlayer offplayer, ETransferResult result) {
				final Player player = offplayer.getPlayer();
				switch(result) {
				case SUCCEED:
					player.sendMessage(LanguageManager.getChatLang(1201004));
					break;
				case FAIL:
				case FORCED_CANCEL:
					if(player != null) player.sendMessage(LanguageManager.getChatLang(1201005));
					break;
				case CANCEL:
					if(player != null) player.sendMessage(LanguageManager.getChatLang(1201006));
					break;
				case UNSAFE:
					askPlayerUnSafe(player, this.land1);
					break;
				default:
				}
			}
		};
		ITransferRequestManager.getITransferRequestManager().transferPlayer(player, landpos, transfer_wait, check_safe, handler);
		if(transfer_wait > 0) {
			player.sendMessage(LanguageManager.getChatLang(1201002, transfer_wait));
		}else {
			player.sendMessage(LanguageManager.getChatLang(1201003));
		}
	}
	private static void askPlayerUnSafe(final Player player,final Land land) {
		IInputRequestManager.getIInputRequestManager().requestInput(player, "gosafe", ConfigurationManager.View_LandInfo_Transfer_NoSafe,
				false, new IInputResultHandler() {
			@Override
			public boolean onInputResult(EInputResult result, OfflinePlayer offplayer, @Nullable String str) {
				boolean cancel = true;
				if(EInputResult.INPUT.equals(result)) transferLand((Player)offplayer, land, false, false);
				LandViewUtil.inputResultResponse(result, offplayer, cancel, 1201008);
				return cancel;
			}
		});
		player.sendMessage(LanguageManager.getChatLang(1201007));
	}
}